package controller

import (
	"fmt"
	"hft/Database/admin"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Room struct{}

func (*Room) CreateRoom(c *gin.Context) {
	var data *admin.Room
	c.ShouldBindJSON(&data)

	fmt.Println(tools.JsonParse(data))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.RoomService.CreateRoom(claims.BaseClaims, data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Room) EditRoom(c *gin.Context) {
	var data *admin.Room
	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.RoomService.EditRoom(claims.BaseClaims, data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Room) GetRoomList(c *gin.Context) {
	louPanID, _ := strconv.Atoi(c.DefaultQuery("loupanId", "0"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.RoomService.GetRoomList(louPanID, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)

}

func (*Room) GetRoomInfo(c *gin.Context) {
	roomId, _ := strconv.Atoi(c.DefaultQuery("roomId", "0"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.RoomService.GetRoomInfo(claims.BaseClaims, roomId)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

var RoomInfoController = new(Room)
